import Utils from "./Utils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MusicManager extends cc.Component {

    public static instance: MusicManager = null;

    @property(cc.Camera)
    camera: cc.Camera = null;

    @property(cc.AudioClip)
    public btnMove: cc.AudioClip = null;

    /**
     * ATM机生成矿石
     */
    @property(cc.AudioClip)
    Atm: cc.AudioClip = null;

    /**
     * 主基地被攻击
     */
    @property(cc.AudioClip)
    baseUnderAtk: cc.AudioClip = null;

    /**
     * 构建设施
     */
    @property(cc.AudioClip)
    build_build: cc.AudioClip = null;

    /**
     * 设施升级
     */
    @property(cc.AudioClip)
    build_up: cc.AudioClip = null;

    /**
     * 设施摧毁
     */
    @property(cc.AudioClip)
    build_die: cc.AudioClip = null;

    /**
     * 击杀
     */
    @property(cc.AudioClip)
    kill_Player: cc.AudioClip = null;

    /**
     * 近防炮开火
     */
    @property(cc.AudioClip)
    defenseCannonFire: cc.AudioClip = null;

    /**
     * 近防炮子弹爆炸
     */
    @property(cc.AudioClip)
    defenseCannonBomb: cc.AudioClip = null;

    /**
     * 电磁炮蓄力
     */
    @property(cc.AudioClip)
    giantLoad: cc.AudioClip = null;

    /**
     * 电磁炮发射
     */
    @property(cc.AudioClip)
    giantFire: cc.AudioClip = null;

    /**
     * 电磁炮爆炸
     */
    @property(cc.AudioClip)
    giantBomb: cc.AudioClip = null;

    /**
     * 榴弹炮发射
     */
    @property(cc.AudioClip)
    howitzerFire: cc.AudioClip = null;

    /**
     * 榴弹炮爆炸
     */
    @property(cc.AudioClip)
    howitzerBomb: cc.AudioClip = null;

    /**
     * 照明弹发射
     */
    @property(cc.AudioClip)
    iproFire: cc.AudioClip = null;

    /**
     * 照明弹爆炸
     */
    @property(cc.AudioClip)
    iproBomb: cc.AudioClip = null;

    /**
     * 火箭炮发射
     */
    @property(cc.AudioClip)
    rocketFire: cc.AudioClip = null;

    /**
     * 火箭炮爆炸
     */
    @property(cc.AudioClip)
    rocketBomb: cc.AudioClip = null;

    /**
     * 洲际导弹发射
     */
    @property(cc.AudioClip)
    missileFire: cc.AudioClip = null;

    /**
     * 洲际导弹爆炸
     */
    @property(cc.AudioClip)
    missileBomb: cc.AudioClip = null;

    /**
     * 核弹发射
     */
    @property(cc.AudioClip)
    nuclearFire: cc.AudioClip = null;

    /**
     * 核弹爆炸
     */
    @property(cc.AudioClip)
    nuclearBomb: cc.AudioClip = null;

    /**
     * 镭射炮
     */
    @property(cc.AudioClip)
    laser: cc.AudioClip = null;

    /**
     * 镭射炮
     */
    @property(cc.AudioClip)
    repair: cc.AudioClip = null;

    /**
     * 黑洞
     */
    @property(cc.AudioClip)
    blackHole: cc.AudioClip = null;

    loopAudio: Map<number, cc.Node>;

    visibleSize: cc.Size

    onLoad() {
        MusicManager.instance = this;
        this.visibleSize = cc.view.getVisibleSize();
        this.loopAudio = new Map<number, cc.Node>();
        this.schedule(this.updateLoopAudio.bind(this), 0.5, cc.macro.REPEAT_FOREVER);
    }

    protected onDestroy(): void {
        this.unscheduleAllCallbacks();
        this.loopAudio.clear();
        cc.audioEngine.stopAll();
    }

    play(clip: cc.AudioClip, sourceNode?: cc.Node, loop: boolean = false) {
        if (loop) {
            if (sourceNode) {
                let audioId = cc.audioEngine.playEffect(clip, loop);
                this.loopAudio.set(audioId, sourceNode);
                sourceNode.on("active-in-hierarchy-changed", () => {
                    if (!sourceNode.activeInHierarchy) {
                        sourceNode.targetOff(this);
                        cc.audioEngine.stopEffect(audioId);
                        this.loopAudio.delete(audioId);
                    }
                }, this);
            }
        } else {
            if (sourceNode) {
                // 检查节点的x坐标是否在屏幕的左右边界之内
                if (!Utils.checkInView(sourceNode, this.camera)) {
                    return;
                }
            }
            cc.audioEngine.playEffect(clip, loop);
        }
    }

    /**
     * 更新循环的音效
     */
    protected updateLoopAudio(): void {
        this.loopAudio.forEach((v, k) => {
            if (Utils.checkInView(v, this.camera)) {
                cc.audioEngine.resumeEffect(k);
            } else {
                cc.audioEngine.pauseEffect(k)
            }
        })
    }
}
